package com.ssm.Utils;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBCPUtils {
    private static Properties properties = new Properties();
    private static DataSource dataSource;
    public static PreparedStatement pstmt = null;
    public static Connection connection = null;
    public static ResultSet rs = null;

    static {
        InputStream in = DBCPUtils.class.getClassLoader().getResourceAsStream("dbcp.properties");
        try {
            properties.load(in);
            dataSource = new BasicDataSourceFactory().createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //返回连接池对象
    public static DataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection() {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }


    public static PreparedStatement createPreparedStatement(String sql, Object[] params)
            throws SQLException {
        pstmt = getConnection().prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        return pstmt;
    }

    public static void closeAll(ResultSet rs, Statement pstmt, Connection connection) {
        try {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }

            if (connection != null) {
                connection.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static boolean executeUpdate(String sql, Object[] params) {
        try {
             int count = createPreparedStatement(sql, params).executeUpdate();
            connection.commit();
            if (count > 0)
                return true;
            else
                return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            DBCPUtils.closeAll(rs, pstmt, connection);
        }
    }

    public static ResultSet executeQuery(String sql, Object[] params) {
        try {
            rs = createPreparedStatement(sql, params).executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }
    public static ResultSet executeQuery(String sql) {
        try {
            rs = createPreparedStatement(sql,null).executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public static int getTotalCount(String sql){
        int count=-1;
        try{
            pstmt=createPreparedStatement(sql,null);
            rs=pstmt.executeQuery(sql);
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs,pstmt,connection);
        }
        return count;
    }
}


